package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UnivAudienceHelper extends NewAudienceHelper {

	private static final long serialVersionUID = 3047719449574506564L;

	public UnivAudienceHelper(ORMInterface orm) {
		List<Universities> univList = new UniversitiesDAO(orm)
				.getAllUniversities();
		this.univList.add(univList);
	}

	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		List<IModel<Universities>> temp = univList.getObject();
		for (IModel<Universities> t : temp) {
			Set<String> keys = roles.keySet();
			for (String r : keys)
				list.add("[" + r +" of " +t.getObject().getTitle() + "]; ");
		}
		return list;
	}

	public IModel<Universities> getUniv(String s) {
		List<IModel<Universities>> ci = univList.getObject();
		for (IModel<Universities> c : ci) {
			if (s.contains(c.getObject().getTitle()) && s.contains("[")
					&& s.contains("]"))
				return c;
		}
		return null;
	}

	public CayenneList<Users> getAllUsers(String s) {
		IModel<Universities> ci = getUniv(s);
		Set<Entry<String, Integer>> set = roles.entrySet();
		for (Entry<String, Integer> e : set) {
			if (s.contains(e.getKey())) {
				CayenneList<Users> l = new UnivDataHandler(ci)
						.getPeopleByRole(e.getValue());
				return l;
			}
		}
		return new CayenneList<Users>(null);
	}

	public CayenneList<GenericAudience> getAudiences(String s) {
		return null;
	}

	public static HashMap<String, Integer> roles = new HashMap<String, Integer>();
	static {
		roles.put("Administrators", UserRoles.ADMIN_ROLE);
		roles.put("Faculty", UserRoles.TEACHER_ROLE);
		roles.put("Students", UserRoles.STUDENT_ROLE);
	}

	private UniqueElementList<Universities> univList = new UniqueElementList<Universities>(
			null);

	@Override
	public String getNameForAudience(String s) {
		Set<String> set = roles.keySet();
		for (String role : set) {
			if (s.contains(role)) {
				return role + ", " + getUniv(s).getObject().getTitle();
			}
		}
		return "";
	}

	@Override
	public int getRoleId(String s) {
		Set<String> set = roles.keySet();
		for (String role : set) {
			if (s.contains(role)) {
				return roles.get(role);
			}
		}
		return 0;
	}
}
